package com.gasstion.controller;

import com.gasstion.dao.*;
import com.gasstion.entity.*;
import com.gasstion.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
@Scope("prototype")
public class GoodsController {
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    GoodsTypeDao goodsTypeDao;
    @Autowired
    SupplierDao supplierDao;
    @Autowired
    WarehouseDao warehouseDao;
    @Autowired
    GoodsService goodsService;


    @RequestMapping("/query")
    @ResponseBody
    public List<Goods> goodsQuery(){
       return  goodsDao.query();
    }

    @RequestMapping("/toadd")
    @ResponseBody
    public Map<String,Object> typeQuery(HttpServletRequest request, HttpServletResponse response){

        Map<String,Object> map = new HashMap<>();

        List<GoodsType> typeList = goodsTypeDao.query();
        List<Supplier> subList = supplierDao.query();
        List<Warehouse> wareList = warehouseDao.query();

        map.put("typeList",typeList);
        map.put("subList",subList);
        map.put("wareList",wareList);
        return map;
    }


    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String,Object> querySearch(String name, int offset, int limit){
        List<Goods> list = goodsService.findAll(name,offset,limit) ;
        long count = goodsService.findTotalCount(name);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",list) ;
        map.put("total",count) ;
        return  map ;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(Goods goods,int pn){
        goodsDao.save(goods);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",goodsService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",goodsService.findTotalCount("")) ;
        return map;
    }


    @RequestMapping("/del")
    @ResponseBody
    public   Map<String,Object> del(String ids,int pn){
        String[] ss = ids.split("-");
        Integer[] myids = new Integer[ss.length] ;
        for(int i=0;i<ss.length;i++){
            System.out.println(ss[i]);
            myids[i] = Integer.parseInt(ss[i]) ;
        }
        goodsDao.del(myids);
        Map<String,Object> map = new HashMap<>() ;
        int limit = 0 ;
        long tcount = goodsDao.getTotalCount("") ;
        long pg = 0 ;
        if(tcount % 2 ==0){
            pg = tcount / 2 ;
        }
        else{
            pg = tcount / 2 +1 ;
        }

        if(pg==pn){
            limit = (pn-1) * 2 ;
        }
        else{
            limit =  (pn-2) * 2 ;
        }
        map.put("rows",goodsService.findAll("",limit,2)) ;
        map.put("total",goodsService.findTotalCount("")) ;
        return map;
    }



}
